//
//  WmcUserAvatarView.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/13.
//

import Kingfisher
import UIKit

extension String {
    /// 截取到任意位置
    func subString(to: Int) -> String {
        let index: String.Index = self.index(startIndex, offsetBy: to)
        return String(self[..<index])
    }
}

class WmcMemberAvatarView: WmcBaseView {
    var member: WmcTeamMember? {
        didSet {
            guard let member = member else { return }
            if member.img != nil, member.img != "" {
                if member.img == "my_cloud_ablum_icon" {
                    imgView.image = UIImage(named: member.img ?? "")
                } else {
                    if let imgUrl = URL(string: member.img ?? "") {
                        imgView.kf.setImage(with: .network(Kingfisher.ImageResource(downloadURL: imgUrl)),
                                            placeholder: "image_placeholder".wmc_image,
                                            completionHandler: nil)
                    }
                }
                self.nameLabel.isHidden = true
                self.imgView.isHidden = false
            } else {
                self.nameLabel.text = member.showName
                self.imgView.isHidden = true
                self.nameLabel.isHidden = false
            }
        }
    }

    override func makeUI() {
        addSubview(imgView)
        addSubview(nameLabel)

        layer.cornerRadius = 4
        clipsToBounds = true

        imgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        nameLabel.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
    }

    /// 用户头像
    private lazy var imgView: UIImageView = {
        let view = UIImageView()
        return view
    }()

    /// 用户名称
    lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.backgroundColor = UIColor.wmc_mainTheme
        view.adjustsFontSizeToFitWidth = true
        view.textColor = UIColor.wmc_white_FFFFFF
        view.textAlignment = .center
        view.font = 14.yp.uiFont
        view.text = "张三"
        return view
    }()
}

extension WmcMemberAvatarView {}
